package cn.macswelle.campusecurity.sdk.service;

import lombok.SneakyThrows;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class DeviceThread implements Runnable {
  public volatile Boolean running = true;
  public volatile Boolean paused = false;
  public final Object lock = new Object();
  protected String info = null;
  protected final Logger logger = LoggerFactory.getLogger(this.getClass());

  public DeviceThread() {
  }

  public final void resumeThread() {
    this.paused = false;
    synchronized (lock) {
      lock.notify();
    }
  }

  @SneakyThrows
  @Override
  public final void run() {
    if (!preExecute()) return;
    while (running) {
      if (paused) synchronized (lock) {
        lock.wait();
      }
      execute();
    }
    postExecute();
  }

  public abstract void execute();

  public abstract boolean preExecute();

  public abstract void postExecute();
}
